using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;
using System.Collections;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;

namespace Microsoft.AceEngineering.SPM.SPMDataLayer
{
    public class AttributesDL
    {
        public static List<PropertyCDE> GetAttributes(bool active, string dir)
        {
            SqlDataReader sqlreader = null;
            List<PropertyCDE> list = new List<PropertyCDE>();
            PropertyCDE prop = null;

            try
            {
                string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

                SqlParameter[] myParams = new SqlParameter[2];
                myParams[0] = new SqlParameter("@Active", SqlDbType.Bit);
                myParams[0].Value = active;

                myParams[1] = new SqlParameter("@dir", SqlDbType.VarChar, 20);
                myParams[1].Value = GiveMeDir(dir);

                sqlreader = SqlHelper.ExecuteReader(conString, CommandType.StoredProcedure, "SPM_spProperties", myParams);
                while (sqlreader.Read())
                {
                    prop = new PropertyCDE();
                    prop.AttributeID = sqlreader[1].ToString();
                    prop.AttributeName = sqlreader[0].ToString();
                    list.Add(prop);
                    prop = null;
                }
            }
            finally
            {
                if (sqlreader != null)
                {
                    if (!sqlreader.IsClosed)
                        sqlreader.Close();
                }
            }
            return list;
        }

        public static void UpdateAttribute(string attributeID, string modifiedBy, bool active)
        {
            string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

            SqlParameter[] myParams = new SqlParameter[3];
            myParams[0] = new SqlParameter("@AttributeID", SqlDbType.VarChar, 200);
            myParams[0].Value = attributeID;

            myParams[1] = new SqlParameter("@user", SqlDbType.VarChar, 50);
            myParams[1].Value = modifiedBy;

            myParams[2] = new SqlParameter("@Active", SqlDbType.Bit);
            myParams[2].Value = active;

            SqlHelper.ExecuteNonQuery(conString, CommandType.StoredProcedure, "SPM_spUpdateAttributesStatus", myParams);
        }

        private static string GiveMeDir(string sortExp)
        {
            if (sortExp.Contains("DESC"))
                return "DESC";
            else
                return "ASC";
        }
    }
}
